package com.tianqu.tms.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tianqu.tms.core.base.TmsEntity;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.dao.TmsDao;
import com.tianqu.tms.dao.VendorDao;
import com.tianqu.tms.dto.CarStyleCountDto;
import com.tianqu.tms.dto.MarketCarPriceCountDto;
import com.tianqu.tms.dto.PageResultDto;
import com.tianqu.tms.dto.VendorDto;

/**
 * 车辆品牌字典表
 * 
 * @author tianshifu
 * 
 */

public class Vendor extends TmsEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8528495424028755724L;

	private String vendorId;// 品牌ID

	private String vendorName;// 品牌名
	
	private String country;//国别
	
	private String firstChar;//品牌首字母

	/**
	 * 实例化codes表
	 * 
	 * @return
	 */
	private static Vendor instance;

	List<Vendor> codes = null;
	
	/**
	 * 加载Codes
	 * 
	 * @return
	 * @throws TmsDaoException
	 */
	public static Vendor getInstance() throws TmsDaoException {
		if (null == instance) {
			instance = new Vendor();
			instance.init();
		}
		return instance;
	}

	/**
	 * 实例化Code
	 * 
	 * @throws TmsDaoException
	 */
	public void init() throws TmsDaoException {
		codes = new ArrayList<Vendor>();
		setCodes();
	}
	
	public VendorDto getVendorInfo(String id) throws TmsDaoException{
		VendorDao dao = (VendorDao) TmsDao.getInstance(TmsDao.TMS_VENDOR);
		return dao.getVendorInfo(id);
	}
	
	public int findAllVendorInfoCountTotal() throws TmsDaoException {
		VendorDao dao = (VendorDao) TmsDao.getInstance(TmsDao.TMS_VENDOR);
		return dao.findAllVendorInfoCountTotal();
	}
	
	public int findOnlineCount()  throws TmsDaoException {
		VendorDao dao = (VendorDao) TmsDao.getInstance(TmsDao.TMS_VENDOR);
		return dao.findOnlineCount();
	}
	
	public List<VendorDto> findAllVendorInfoCount()  throws TmsDaoException {
		VendorDao dao = (VendorDao) TmsDao.getInstance(TmsDao.TMS_VENDOR);
		return dao.findAllVendorInfoCount();
	}
	
	public List<CarStyleCountDto> getCarStyleCount(String vendorId) throws TmsDaoException {
		VendorDao dao = (VendorDao) TmsDao.getInstance(TmsDao.TMS_VENDOR);
		return dao.getCarStyleCount(vendorId);
	}
	
	public PageResultDto getPageCarStyleCount(String vendorId,int pageSize,int pageNumber) throws TmsDaoException {
		PageResultDto page = new PageResultDto();
		VendorDao dao = (VendorDao) TmsDao.getInstance(TmsDao.TMS_VENDOR);
		List<CarStyleCountDto> datas = dao.getCarStyleCount(vendorId);
		int dataCount = datas.size() -1;
		int startNum = pageNumber * pageSize - pageSize;
		int endNum = pageNumber * pageSize;
		int i =0;
		List<CarStyleCountDto> result = new ArrayList<CarStyleCountDto>();
		while (i <= dataCount) {
			CarStyleCountDto dto = datas.get(i);
			if(i %2 != 0) dto.setCssFlag(2);
			if(i < startNum || i > endNum){i++;continue;
			}else{ result.add(dto);i++; }
		}
		page.setPageNumber(pageNumber);
		page.setPageSize(pageSize);
		page.setRecordCount(datas.size());
		page.setResult(result);
		return page;
	}
	
	public PageResultDto getPage(int pageSize,int pageNumber) throws TmsDaoException {
		PageResultDto page = new PageResultDto();
		VendorDao dao = (VendorDao) TmsDao.getInstance(TmsDao.TMS_VENDOR);
		List<VendorDto> datas =  dao.findVendorOrderByFirstChar();
		int dataCount = datas.size() -1;
		int startNum = pageNumber * pageSize - pageSize;
		int endNum = pageNumber * pageSize;
		int i =0;
		List<VendorDto> result = new ArrayList<VendorDto>();
		while (i <= dataCount) {
			if(i < startNum || i > endNum){
				i++;continue;
			}else{
				result.add(datas.get(i));i++;
			}
		}
		page.setPageNumber(pageNumber);
		page.setPageSize(pageSize);
		page.setRecordCount(datas.size());
		page.setResult(result);
		return page;
	}
	
	/**
	 * 获取Codes信息存在静态codes中
	 * @throws TmsDaoException
	 */
	public void setCodes() throws TmsDaoException {
		VendorDao dao = (VendorDao) TmsDao.getInstance(TmsDao.TMS_VENDOR);
		List<VendorDto> list = dao.findAllVendorInfo();
		for (int i = 0; i < list.size(); i++) {
			VendorDto dto = (VendorDto) list.get(i);
			Vendor entity = new Vendor();
			entity.setVendorId(dto.getVendorId());
			entity.setVendorName(dto.getVendorName());
			entity.setCountry(dto.getCountry());
			entity.setFirstChar(dto.getFirstChar());
			codes.add(entity);
		}
	}
	 
	/**
	 * 获取下拉列表
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List findAllVendorInfo(){
		List list = new ArrayList();
		Map map = null;
		for(int i=0;i<codes.size();i++){
			Vendor vendor = codes.get(i);
			map = new HashMap();
			map.put("vendorId", vendor.getVendorId());
			map.put("vendorName", vendor.getVendorName());
			list.add(map);
		}
		return list;
	}

	public List<MarketCarPriceCountDto> getMarketCarPriceCount() throws TmsDaoException{
		VendorDao dao = (VendorDao) TmsDao.getInstance(TmsDao.TMS_VENDOR);
		return dao.getMarketCarPriceCount();
	}

	public String getVendorId() {
		return vendorId;
	}

	public void setVendorId(String vendorId) {
		this.vendorId = vendorId;
	}

	public String getVendorName() {
		return vendorName;
	}

	public void setVendorName(String vendorName) {
		this.vendorName = vendorName;
	}
	

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getFirstChar() {
		return firstChar;
	}

	public void setFirstChar(String firstChar) {
		this.firstChar = firstChar;
	}

	@Override
	public void save() throws TmsDaoException, TmsException {
		// TODO Auto-generated method stub

	}

}
